package com.topnav.mc.admin.service.impl;

import java.io.Serializable;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.js.lib.cache.Cache;
import com.topnav.mc.admin.entity.Device;
import com.topnav.mc.admin.entity.bean.GbServerBean;
import com.topnav.mc.admin.entity.param.GbServerParam;
import com.topnav.mc.admin.service.IDeviceService;
import com.topnav.mc.base.mc.McServiceImpl;
import com.topnav.mc.common.Constant;
import static com.topnav.mc.exception.ExceptionMsg.*;
import com.topnav.mc.uitls.ErrorResultUtil;
import com.topnav.mc.uitls.ExceptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.js.lib.exception.CustomException;
import com.topnav.mc.admin.dao.mapper.GbServerMapper;
import com.topnav.mc.admin.entity.GbServer;
import com.topnav.mc.admin.service.IGbServerService;
import javax.annotation.Resource;

@Service
public class GbServerServiceImpl extends McServiceImpl<GbServerMapper, GbServer, GbServerBean, GbServerParam> implements IGbServerService {
	@Resource(name="sysCache")
	private Cache sysCache;

	@Autowired
	private IDeviceService deviceService;

	@Override
	public boolean beforeSave(GbServer entity){
		if (this.getOne(new QueryWrapper<GbServer>().eq("code",entity.getCode()), true) != null){
			throw ExceptionUtil.commonException(ERROR_PARAM_GBSERVER_EXISTS);
		}
		entity.setCreateTime(new Date());
		entity.setUpdateTime(new Date());
		return true;
	}

	@Override
	public  void afterSave(GbServer entity){
		this.saveToCache(entity);
	}
	@Override
	public   void afterUpdate(GbServer entity){
		this.saveToCache(entity);
	};
	@Override
	public  void afterDelete(GbServer entity){
		this.saveToCache(entity);
	};

	@Override
	public boolean beforeUpdate(GbServer entity){
		QueryWrapper<GbServer> wrapper = new QueryWrapper<GbServer>()
				.eq("code", entity.getCode()).ne("id", entity.getId());
		if (this.getOne(wrapper, true) != null){
			throw ExceptionUtil.commonException(ERROR_PARAM_GBSERVER_EXISTS);
		}
		entity.setUpdateTime(new Date());
		return true;

	}
	@Override
	public  boolean beforeDelete(GbServer gbServer){
		LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<Device>().eq(Device::getGbServerId,gbServer.getId());
		if (deviceService.getOne(queryWrapper,false) != null){
			throw ExceptionUtil.commonException(ERROR_DELETE_GBSERVER_USED_BY_DEVICE);
		}

		return true;
	}


    /**
     * 查询列表
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
	@Override
	public List<GbServer> list(Wrapper<GbServer> queryWrapper) {
		QueryWrapper<GbServer> wrapper = new QueryWrapper<GbServer>();
		if (queryWrapper.getEntity() != null){
			if (queryWrapper.getEntity().getId()!=null){
				wrapper.eq("id", queryWrapper.getEntity().getId());
			}

			if (!StringUtils.isEmpty(queryWrapper.getEntity().getCode())){
				wrapper.like("code", queryWrapper.getEntity().getCode());
			}
			if (!StringUtils.isEmpty(queryWrapper.getEntity().getName())){
				wrapper.like("name", queryWrapper.getEntity().getName());
			}
			if (!StringUtils.isEmpty(queryWrapper.getEntity().getHost())){
				wrapper.like("host", queryWrapper.getEntity().getHost());
			}
		}

        return getBaseMapper().selectList(wrapper);
    }

    /**
     * 翻页查询
     *
     * @param page         翻页对象
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
	@Override
	public <E extends IPage<GbServer>> E page(E page, Wrapper<GbServer> queryWrapper) {
		QueryWrapper<GbServer> wrapper = new QueryWrapper<GbServer>();
		if (queryWrapper.getEntity() != null){
			if (!StringUtils.isEmpty(queryWrapper.getEntity().getCode())){
				wrapper.like("code", queryWrapper.getEntity().getCode());
			}
			if (!StringUtils.isEmpty(queryWrapper.getEntity().getName())){
				wrapper.like("name", queryWrapper.getEntity().getName());
			}
			if (!StringUtils.isEmpty(queryWrapper.getEntity().getHost())){
				wrapper.like("host", queryWrapper.getEntity().getHost());
			}
		}
        return getBaseMapper().selectPage(page, wrapper);
    }


	@Override
	public boolean removeById(Serializable id) {
		return super.removeById(id);
	}



	/**
	 * 保存到缓存
	 * @param gs
	 */
	@Override
	public void saveToCache(GbServer gs) {
		sysCache.hset(Constant.CACHE_KEY_GBSERVER, gs.getId().toString(), gs);
	}

	@Override
	public void removeFromCache(GbServer gs) {
		if (gs != null && gs.getId() != null)
			sysCache.hdel(Constant.CACHE_KEY_GBSERVER, gs.getId().toString());
	}

	@Override
	public void saveAllToCache() {
		sysCache.del(Constant.CACHE_KEY_GBSERVER);
		List<GbServerBean> list = this.listInfo();
		for(GbServerBean g:list){
			this.saveToCache(g);
		}
	}

	/**
	 * 从缓存存取出
	 * @param id
	 * @return
	 */
	@Override
	public GbServer getFromCache(String id){
		return (GbServer) sysCache.hget(Constant.CACHE_KEY_GBSERVER, id);
	}

	@Override
	public List<GbServer> getAllFromCache(){
		List<GbServer> list = new ArrayList<>();
		Map<Object,Object> map =  sysCache.hmget(Constant.CACHE_KEY_GBSERVER);
		if (map != null) {
			for (Object obj : map.values()) {
				list.add((GbServer) obj);
			}
		}
/*		Iterator<Map.Entry<Object,Object>> iterator = map.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry<Object,Object> entry = iterator.next();
			list.add((GbServer)entry.getValue());
		}*/
		return list;
	}

}
